<?php
namespace app\admin\controller;

use think\Config;
use think\Request;
use app\common\controller\Base;

class Route extends Base{

    protected $routeMap = [];
    protected $routerFile = 'routermap';

    public function __construct(Request $request){
        parent::__construct($request);

        $this->routerFile = Config::get("router_file");
        $this->routeMap = require APP_PATH."../public/".$this->routerFile.".php";
    }

    public function index(){

        return view('',['routeMap'=>$this->routeMap]);
    }

    public function createsitemap(){
        $header = <<<HEAD
<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
        http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
HEAD;
        $foot = <<<FOOT
</urlset>
FOOT;

        $body = '';
        foreach( $this->routeMap as $key => $val ){
            $body .= <<<BODY
    <url>
        <loc>https://www.horseen.com/{$val}</loc>
        <priority>0.5</priority>
        <lastmod>2018-12-4</lastmod>
        <changefreq>weekly</changefreq>
    </url>
BODY;
        }

        $content = $header.$body.$foot;

        //怎么感觉这里应该是get  目前没问题 就先别管吧
        $siteMapFile = Config::set("site_map_file");

        file_put_contents($siteMapFile.".xml",$content);
        $this->success("sitemap生成成功");
    }

    public function delete(Request $request){
        if(!$request->isDelete()){
            $this->error("非法访问途径");
        }

        $id = $request->only(['id']);
        if(count($id) !== 1){
            $this->error("禁止批量操作");
        }


        $id = $id['id'];
        $id = base64_decode($id);


        if(isset($this->routeMap[$id])){
            unset($this->routeMap[$id]);
            $this->save();
        }

        $this->success("操作成功");
    }

    /**
     * 批量添加
     */
    public function add(){

        return view('',[]);
    }

    /**
     * 添加路径
     */
    public function create(Request $request){
        $param = $request->post();
        $route = $param['route'];

        //检查字符串
        $res = preg_match('/^[?0-9a-zA-Z=>,\/_-]+$/',$route,$match);
        if(!$res){
            $this->error("书写不符合规则，不能带引号星号大括号等特殊字符，请参考书写规则");
        }

        $arr = explode(',',$route);

        //一条条检查
        foreach($arr as $val){
            $sub = explode('=>',$val);
            if(count($sub) > 2){
                $this->error($val." 这条不符合规则，请重新书写");
            }
            $this->routeMap[$sub[0]] = $sub[1];
        }

        $this->save();
        $this->success("操作成功");

    }

    private function  save(){
        $before = <<<STR
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/26 0026
 * Time: 上午 7:07
 */
return
STR;

        $end = ";";

        $saveStr = $before.' '.var_export($this->routeMap,true).$end;

        file_put_contents(APP_PATH."../public/".$this->routerFile.".php",$saveStr);

    }
}
